package controllers;

import com.fasterxml.jackson.databind.JsonNode;
import dao.ApplyInfo_Repository;
import play.data.FormFactory;
import play.libs.Json;
import play.libs.concurrent.HttpExecutionContext;
import play.mvc.Controller;
import play.mvc.Result;

import javax.inject.Inject;
import java.util.concurrent.CompletionStage;


/**
 * The controller keeps all database operations behind the repository, and uses
 * {@link HttpExecutionContext} to provide access to the
 * {@link play.mvc.Http.Context} methods like {@code request()} and {@code flash()}.
 */
public class ApplyInfoController extends Controller {

    private final FormFactory formFactory;
	private final ApplyInfo_Repository applyInfo_Repository;
    private final HttpExecutionContext ec;

    @Inject
    public ApplyInfoController(FormFactory formFactory, ApplyInfo_Repository applyInfo_Repository, HttpExecutionContext ec) {
        this.formFactory = formFactory;
        this.applyInfo_Repository =  applyInfo_Repository;
        this.ec = ec;
    }

    public CompletionStage<Result> getApplyList() {
        JsonNode json = request().body().asJson();
        String StartDate = json.findPath("StartDate").textValue();
        String EndDate = json.findPath("EndDate").textValue();
        String PhoneNumber = json.findPath("PhoneNumber").textValue();
        String AuditStatus = json.findPath("AuditStatus").textValue();
        String productName = json.findPath("productName").textValue();

        return applyInfo_Repository.getApplyList(StartDate, EndDate, PhoneNumber, AuditStatus, productName).thenApplyAsync(_GetListByFunc -> {
            return ok(Json.toJson(_GetListByFunc));
        }, ec.current());
    }

    public CompletionStage<Result> getApplyInfoList() {
		JsonNode json = request().body().asJson();
		String StartDate = json.findPath("StartDate").textValue();
		String EndDate = json.findPath("EndDate").textValue();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String AuditStatus = json.findPath("AuditStatus").textValue();
    	
	    return applyInfo_Repository.getApplyInfoList(StartDate, EndDate, PhoneNumber, AuditStatus).thenApplyAsync(_GetListByFunc -> {
    		return ok(Json.toJson(_GetListByFunc));
	    }, ec.current());
    }

/*	public CompletionStage<Result> CreatNewApply() {
		JsonNode json = request().body().asJson();
		List<JsonNode> fd_SubId = json.findValues("fd_SubId");
		List<JsonNode> TruckNoList = json.findValues("TruckNo");
		List<JsonNode> PhoneNumber = json.findValues("PhoneNumber");
		List<JsonNode> fd_DeliveryOrderid = json.findValues("fd_DeliveryOrderid");
		List<JsonNode> DeliveryDate = json.findValues("DeliveryDate");
		List<JsonNode> DeliveryQty = json.findValues("DeliveryQty");
		List<JsonNode> UnLoadPlace = json.findValues("UnLoadPlace");
		List<JsonNode> CirCleTime = json.findValues("CirCleTime");


		return applyInfo_Repository.CreatNewApply(PhoneNumber, fd_DeliveryOrderid, fd_SubId, TruckNoList, DeliveryDate, DeliveryQty, UnLoadPlace, CirCleTime).thenApplyAsync(_GetListByProc -> {
			return ok(Json.toJson(_GetListByProc));
		}, ec.current());
	}*/
public CompletionStage<Result> CreatNewApply(){
	JsonNode json = request().body().asJson();
	String PhoneNumber = json.findPath("PhoneNumber").textValue();
	String fd_DeliveryOrderid = json.findPath("fd_DeliveryOrderid").textValue();
	String fd_SubId = json.findPath("fd_SubId").textValue();
	String TruckNo = json.findPath("TruckNo").textValue();
	String DeliveryDate = json.findPath("DeliveryDate").textValue();
	String DeliveryQty = json.findPath("DeliveryQty").textValue();
	String UnLoadPlace = json.findPath("UnLoadPlace").textValue();
	String CirCleTime = json.findPath("CirCleTime").textValue();

	return applyInfo_Repository.CreatNewApply(PhoneNumber,fd_DeliveryOrderid, fd_SubId, TruckNo, DeliveryDate, DeliveryQty , UnLoadPlace , CirCleTime).thenApplyAsync(_GetListByProc -> {
		return ok(Json.toJson(_GetListByProc));
	}, ec.current());
}

	public CompletionStage<Result> aduit(){
		JsonNode json = request().body().asJson();
		String applicationId = json.findPath("applicationId").textValue();
		String result = json.findPath("result").textValue();
		String reason = json.findPath("reason").textValue();
	    Long bookTime = json.findPath("bookTime").longValue();


		return applyInfo_Repository.aduit(applicationId,result,reason,bookTime).thenApplyAsync(_aduit -> {
			return ok(Json.toJson(_aduit));
		}, ec.current());
	}

	public CompletionStage<Result> cancleApply() {
		JsonNode json = request().body().asJson();
		Integer ApplyID = json.findPath("ApplyID").intValue();

		return applyInfo_Repository.cancleApply(ApplyID).thenApplyAsync(result -> {
			return ok(Json.toJson(result));
		}, ec.current());
	}

	public CompletionStage<Result> pcancleApply() {
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String ApplyNumber = json.findPath("ApplyNumber").textValue();

		return applyInfo_Repository.pcancleApply(PhoneNumber, ApplyNumber).thenApplyAsync(result -> {
			return ok(Json.toJson(result));
		}, ec.current());
	}

	public CompletionStage<Result> AuditApplyBatch() {
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String ApplyIDs = json.findPath("ApplyIDs").textValue();

		return applyInfo_Repository.AuditApplyBatch(PhoneNumber, ApplyIDs).thenApplyAsync(result -> {
			return ok(Json.toJson(result));
		}, ec.current());
	}

	public CompletionStage<Result> cancleApplyBatch() {
		JsonNode json = request().body().asJson();
		String PhoneNumber = json.findPath("PhoneNumber").textValue();
		String ApplyIDs = json.findPath("ApplyIDs").textValue();
		String reason = json.findPath("reason").textValue();

		return applyInfo_Repository.cancleApplyBatch(PhoneNumber, ApplyIDs, reason).thenApplyAsync(result -> {
			return ok(Json.toJson(result));
		}, ec.current());
	}
}
